package org.chandler.ccgp.app;

import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.chandler.ccgp.dao.TaskDAO;
import org.chandler.ccgp.data.Task;
import org.chandler.ccgp.parser.ListParser;
import org.chandler.ccgp.parser.TaskParser;
import org.chandler.ccgp.util.CcgpHttpUtil;
import org.chandler.util.HttpUtil;

public class Catch {
	
	public static final String baseUrl = "http://search.ccgp.gov.cn/dataB.jsp";
	
	public static void main(String args[]){
		//构造get参数
		String startTime = "2015:04:21";
		String endTime = "2015:07:22";
		List<NameValuePair> params = CcgpHttpUtil.GetParams(startTime, endTime);
		//生产url
		String url = CcgpHttpUtil.genUrl(baseUrl, params);
		System.out.println(url);
		//获得list页
		String html = HttpUtil.get(url);
		//解析list页，生成task
		Task t = new Task();
		t.sDate = startTime;
		t.eDate = endTime;
		t.url = url;
		TaskParser.homeParser(html, t);
		//数据库中插入任务
		//t.id = TaskDAO.insertInit(t);
		//按照任务逐页执行
		t.totalPage = 1;//for test
		for (int i = 1; i <= t.totalPage; i++) {
			if(i > 1){
				params.remove(new BasicNameValuePair("page_index",String.valueOf(i-1)));
				params.add(new BasicNameValuePair("page_index",String.valueOf(i)));
				url = CcgpHttpUtil.genUrl(baseUrl, params);
			}
			ListParser.getBiddingList(url);
		}
	}
}
